<?php
include('config.php');




getDialog();




function getKey(){
	$method = 'callTableClassKey';
	$postParams = array(
		'tableName'=>'Mod_Text',
		'columnId'=>'1',
		'columnName'=>'id'
		);
	
	$params = array();
	$params['apiKey']  = API_KEY;
	$params['enc_req'] = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, API_SECRET, json_encode($postParams), MCRYPT_MODE_ECB));
	
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, BASE.'api/'.$method);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	return  curl_exec($ch);
}

function getDialog(){
	/* Need to add a session tokens to fields to stop this kind of hacking */
	
	$resp = json_decode(getKey(), true);
	
	$method = 'tableClassDialog';
	$postParams = array(
		'tableName'=>'Mod_Text',
		'columnId'=>'1',
		'columnName'=>'id'
		);
	
	$params = array();
	$params['key']  = $resp['key'];
	
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, BASE.'ajax/'.$method);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, 1);

	$respAjax = stripslashes(curl_exec($ch));
	
	preg_match('/^Set-Cookie: (.*?);/m', $respAjax, $m);
	
	$url 	= parse_url($m[1]);
	$ppArr = explode('=',$url['path']);
	

	
	preg_match_all('@value="([^"]*)"@', $respAjax, $dee);

	
	//<input name=\"token\" type=\"hidden\" value=\"
	
	
	submit($ppArr[1], $resp['key'], $dee[1][2]);
	
	
}

function submit($phpSessionID, $key, $token){
	
	/* 
		$postVals['datatypeVal'] = $_POST['datatypeVal'];
		$postVals['switch']			 = $_POST['tableClassAction'];		
	*/
	$params = array();
	$params['key']  	= $key;
	$params['token']  = $token;
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, BASE.'ajax/tableClassDialogPost');
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	
	curl_setopt($ch,CURLOPT_COOKIE,"PHPSESSID=".$phpSessionID."; domain=.localhost; path=/");
	
	$respAjax = curl_exec($ch);
	
	echo('<pre>');
	print_r(json_decode($respAjax, true));
	echo('</pre>');
	
	//$phpSessionID
}



?>
